/* Copyright 2017, 2018, Gabriel Czernikier
 *
 * This file is part of Notehaus.
 * Notehaus is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * Notehaus is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with Notehaus.  If not, see <http://www.gnu.org/licenses/>.
 */
#define _BSD_SOURCE
#include <endian.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include "composer.h"
#include "lgwseq.h"
#include "types.h"

LIST_HEAD(proto_id_head);
unsigned int proto_id_list_size = 0;

struct proto_id_st *add_proto_id(struct type_id_st *return_type_idx, struct type_list_st *parameters_list) {
  struct proto_id_st *proto_id = malloc(sizeof(struct proto_id_st));
  proto_id->return_type_idx = return_type_idx;
  proto_id->parameters_list = parameters_list;
  wchar_t *shorty = malloc((1+parameters_list->nb_members)*sizeof(wchar_t));
  *shorty = *return_type_idx->str_id->str_dt->unpacked_data->data;
  int i=0;
  struct list_head *tmp;
  list_for_each(tmp, &parameters_list->type_member_head) {
    // Take a general TypeDescriptor into an array position
    shorty[++i] = *list_entry(tmp, struct type_member_st, type_member_list)->type_id->str_id->str_dt->unpacked_data->data;
    // Convert it to its ShortyDescriptor form
    if(shorty[i]==L'[')
      shorty[i]=L'L';
  }
  lgwseq_t *shorty_2;
  for_lgwseq(&shorty_2, i+1, shorty);
  proto_id->shorty_idx = add_str_dt(shorty_2);
  list_add(&proto_id->proto_id_list, &proto_id_head);
  proto_id_list_size++;
  return proto_id;
}

int oproto_id_st_compar(struct oproto_id_st *oproto_id_1, struct oproto_id_st *oproto_id_2) {
  if(oproto_id_1->proto_id->return_type_idx!=oproto_id_2->proto_id->return_type_idx) {
    return oproto_id_1->proto_id->return_type_idx-oproto_id_2->proto_id->return_type_idx;
  }
  if(oproto_id_1->proto_id->parameters_list->relative_offset!=oproto_id_2->proto_id->parameters_list->relative_offset) {
    return oproto_id_1->proto_id->parameters_list->relative_offset-oproto_id_2->proto_id->parameters_list->relative_offset;
  }
  oproto_id_1->bool_duplicate=oproto_id_2->bool_duplicate=1;
  return 0;
}

void
build_oproto_id() {
  oproto_id_ary = malloc(proto_id_list_size*sizeof(struct oproto_id_st));
  memset(oproto_id_ary, 0, proto_id_list_size*sizeof(struct oproto_id_st));
  struct list_head *tmp;
  unsigned int i=0;
  list_for_each(tmp, &proto_id_head) {
    (oproto_id_ary+i)->proto_id = (struct proto_id_st *)list_entry(tmp, struct proto_id_st, proto_id_list);
    i++;
  }
  qsort(oproto_id_ary, proto_id_list_size, sizeof(struct oproto_id_st), (int (*)(const void *, const void *))oproto_id_st_compar);
  i=1;
  unsigned int major_i = 0, major_idx = 0;   // TODO Consult whether indices are 0 or 1 based. But in the event of a fix, care of not bringing the bounds_move() operation into incorrectness.
  while(major_i<proto_id_list_size) {
    (oproto_id_ary+major_i)->proto_id->idx = major_idx;
    while(i!=proto_id_list_size && (oproto_id_ary+i)->bool_duplicate && oproto_id_st_compar(oproto_id_ary+major_i, oproto_id_ary+i)==0) {
      (oproto_id_ary+i)->duplicate_major = (oproto_id_ary+major_i);
      (oproto_id_ary+i)->proto_id->idx = (oproto_id_ary+major_i)->proto_id->idx;
      i++;
    }
    (oproto_id_ary+major_i)->bool_duplicate=0;   // Holds with the caluse: bool_duplicate can be set after the _compar function that updates this fields was called for the very last time on this position.
    if(i!=proto_id_list_size)
      (oproto_id_ary+major_i)->next_major = oproto_id_ary+i;
    major_i=i++;
    major_idx++;
  }
  bounds_move(NH_PROTO_ID_IDX, 3*sizeof(uint32_t)/*Storage Designators: ((struct str_id_st)(struct proto_id_st).shorty_idx).idx,
  ((struct type_id_st)(struct proto_id_st).return_type_idx).idx,
  ((struct type_list_st)(struct proto_id_st).parameters_list).relative_offset;
  This makes for a total of 3 uint32_t.
  */ *major_idx);
  majors_size_ary[NH_PROTO_ID_IDX] = major_idx;
}

void pack_oproto_id() {
  for(struct oproto_id_st *oproto_id_major = oproto_id_ary;oproto_id_major!=NULL; oproto_id_major=oproto_id_major->next_major){
    uint32_t packed = htole32(oproto_id_major->proto_id->shorty_idx->idx);
    BUFFER_WRITE(buffer[NH_PROTO_ID_IDX],buf_len[NH_PROTO_ID_IDX],&packed,buf_offset[NH_PROTO_ID_IDX],sizeof(uint32_t))
    packed = htole32(oproto_id_major->proto_id->return_type_idx->idx);
    BUFFER_WRITE(buffer[NH_PROTO_ID_IDX],buf_len[NH_PROTO_ID_IDX],&packed,buf_offset[NH_PROTO_ID_IDX],sizeof(uint32_t))
    packed = htole32(oproto_id_major->proto_id->parameters_list->relative_offset+up_bounds_ary[NH_TYPE_LIST_IDX-1]);
    BUFFER_WRITE(buffer[NH_PROTO_ID_IDX],buf_len[NH_PROTO_ID_IDX],&packed,buf_offset[NH_PROTO_ID_IDX],sizeof(uint32_t))
  }
}
